package com.stylefeng.guns.modular.system.controller;

import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.modular.system.model.ChannelSon;
import com.stylefeng.guns.modular.system.service.IChannelSonService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.system.model.Jarinfo;
import com.stylefeng.guns.modular.system.service.IJarinfoService;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * 内核升级管理控制器
 *
 * @author fengshuonan
 * @Date 2018-10-18 10:38:05
 */
@Controller
@RequestMapping("/jarinfo")
public class JarinfoController extends BaseController {

    private String PREFIX = "/system/jarinfo/";

    @Autowired
    private IJarinfoService jarinfoService;

    @Autowired
    private IChannelSonService iChannelSonService;

    /**
     * 跳转到内核升级管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "jarinfo.html";
    }

    /**
     * 跳转到添加内核升级管理
     */
    @RequestMapping("/jarinfo_add")
    public String jarinfoAdd(Model model) {
        //Integer id = jarinfoService.selectMaxId();
        //model.addAttribute("cipd",getCpid(id));
        //System.out.println(getCpid(id));
        return PREFIX + "jarinfo_add.html";
    }

    public String getCpid(Integer id){
        String index = "";
        int length = (id+"").length();
        for(int i=5;i>length;i--){
            index += "0";
        }
        return "HW_Q_"+index+(id+1);
    }

    /**
     * 跳转到修改内核升级管理
     */
    @RequestMapping("/jarinfo_update/{jarinfoId}")
    public String jarinfoUpdate(@PathVariable Integer jarinfoId, Model model) {
        Jarinfo jarinfo = jarinfoService.selectById(jarinfoId);
        model.addAttribute("item",jarinfo);
        LogObjectHolder.me().set(jarinfo);
        return PREFIX + "jarinfo_edit.html";
    }

    /**
     * 获取内核升级管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return jarinfoService.selectList(null);
    }

    /**
     * 新增内核升级管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Jarinfo jarinfo) {
        if(jarinfo.getCpid()==null||jarinfo.getCpid().equals("")){
            Integer id = jarinfoService.selectMaxId();
            jarinfo.setCpid(getCpid(id));
        }
        jarinfoService.insert(jarinfo);
        return SUCCESS_TIP;
    }

    /**
     * 删除内核升级管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer jarinfoId) {
        jarinfoService.deleteById(jarinfoId);
        return SUCCESS_TIP;
    }

    /**
     * 修改内核升级管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Jarinfo jarinfo) {
        jarinfoService.updateById(jarinfo);
        return SUCCESS_TIP;
    }

    /**
     * 内核升级管理详情
     */
    @RequestMapping(value = "/detail/{jarinfoId}")
    @ResponseBody
    public Object detail(@PathVariable("jarinfoId") Integer jarinfoId) {
        return jarinfoService.selectById(jarinfoId);
    }

    /**
     * 一键生成子渠道
     */
    @RequestMapping(value = "/generator")
    @ResponseBody
    public Object generatChannelSon(@RequestParam Integer jarinfoId) {
        ChannelSon cs = null;
        boolean bool = true;
        try {
            if (jarinfoId != null) {
                Jarinfo jarinfo = jarinfoService.selectById(jarinfoId);
                if (jarinfo != null) {
                    for (int i = 1; i < jarinfo.getChannelSonNumber() + 1; i++) {
                        cs = new ChannelSon();
                        cs.setChannel(jarinfo.getCpid() +"_"+ i);
                        cs.setChannelFatherId(jarinfoId);
                        cs.setIsUse("1");
                        cs.setDate(new Date());
                        iChannelSonService.insert(cs);
                    }
                }
            }
        }catch (Exception e){
            bool = false;
            e.printStackTrace();
        }finally {
            if(bool){
                jarinfoService.updIsGenerator(jarinfoId);
            }
            cs = null;
        }
        return SUCCESS_TIP;
    }

}
